﻿var username;
var secretkey;
var staff_id;
(function($){
	$(window).load(function(){
		username = $.session.get("username");
		secretkey = $.session.get("user_secretkey");
		staff_id = $.session.get("staff_id");
		find_driver_shop();		//司机
		find_staff_managementById();
		
	})
})(jQuery);

function find_staff_managementById(){
	$.ajax({
		url: "../../../../service",
		//async:false,
		//cache:false,
		data : {
			proc : "g_xmy.find_driver_managementById",
			para : "['" + username + "','" + secretkey + "','" + staff_id + "']"
		},
		dataType : "json",
		success : function(data) {
		//	console.log(JSON.stringify(data));
			if (data[0].TAG == "0") {
				window.location.href = "./error.html";
			} else {
				document.getElementById("login_name").value = data[0].NAME;
				document.getElementById("staff_name").value = data[0].NICKNAME;
				document.getElementById("gw_style").value = data[0].TYPE;
				document.getElementById("sj_password").value = data[0].SJ_KEY;
				
				//店铺
				var a = $("#vips_2").find("input");
				var storeids="";
				storeids=storeids.split(",");
				for(var b=0;b<=a.length-1;b++){
					var s=$(a[b]).val();
					for(var ss=0;ss<data.length;ss++){
						if(data[ss].STORE_ID==s){
						$(a[b]).attr("checked","checked");
						}
					}
				}
				
				//顺丰网点
				var a2 = $("#vips_3").find("input");
				var storeids2="";
				storeids2=storeids2.split(",");
				for(var b2=0;b2<=a2.length-1;b2++){
					var s2=$(a2[b2]).val();
					for(var ss=0;ss<data.length;ss++){
						if(data[ss].WEBSITEID==s2){
						//console.log("===");
						$(a2[b2]).attr("checked","checked");
						}
					}
					
				}
			}
		}
	});
}

//加盟店全选
function check2(){
	var check = document.getElementById("vip2");
	var s = document.getElementById("vips_2");
	var n = s.getElementsByTagName("input");
	for(i = 0;i < n.length;i++){//循环数组
		if (check.checked == false) {
			n[i].checked = false;
		} else{
			n[i].checked = true;
		}
	}
}
//顺丰网点全选
function check3(){
	var check = document.getElementById("vip3");
	var s = document.getElementById("vips_3");
	var n = s.getElementsByTagName("input");
	for(i = 0;i < n.length;i++){//循环数组
		if (check.checked == false) {
			n[i].checked = false;
		} else{
			n[i].checked = true;
		}
	}
}

//选择司机时获取所有店铺及顺丰网点
function find_driver_shop(){
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
		$.ajax({									//店铺
			url: "../../../../service",
			data: {
				proc: "g_xmy.find_driver_shop",
				para: "['" + username + "','" + secretkey + "']"
			},
			dataType: "json",
			success: function(data) {
				listCount = data.length;
				var len = $("#vips_2").length;
				if(len>0){
					$("#vips_2").empty();
				}
				if(data[0].TAG == "0"){
					window.location.href="./error.html";
				}else{
					for(var a=0;a<data.length;a++){
						var al = document.getElementById("vips_2");
						var div =document.createElement("li");
						div.innerHTML='<label class="check-box" style="float:left;margin-left:20px;"><input type="checkbox" name="hdf" value="'+data[a].ID+'"/><span>'+data[a].SHOP_NAME+'</span></label>'
						al.appendChild(div);
					}
				}
			}
		});
		$.ajax({					//顺丰网点
			url: "../../../../service",
			async:false,
		    cache:false,
			data: {
				proc: "g_xmy.find_driver_sf",
				para: "['" + username + "','" + secretkey + "']"
			},
			dataType: "json",
			success: function(data) {
				listCount = data.length;
				var len = $("#vips_3").length;
				if(len>0){
					$("#vips_3").empty();
				}
				if(data[0].TAG == "0"){
					window.location.href="./error.html";
				}else{
					for(var a=0;a<data.length;a++){						
						var al = document.getElementById("vips_3");
						var div =document.createElement("li");
						div.innerHTML='<label class="check-box" style="float:left;margin-left:20px;"><input type="checkbox" name="hdf" value="'+data[a].ID+'"/><span>'+data[a].NAME+'</span></label>'
						al.appendChild(div);
						
					}
				}
			}
		});
	//	sfwd_2();
}
/*
function sfwd_2(){			//更多顺丰网点查询
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
	$.ajax({								
		url: "../service",
		data: {
			proc: "xiyi_order.find_driver_sf",
			para: "['" + username + "','" + secretkey + "']"
		},
		dataType: "json",
		async:false,
		cache:false,
		success: function(data) {
		//	alert(JSON.stringify(data));
			listCount = data.length;
			var len = $("#vips_3_2").length;
			if(len>0){
				$("#vips_3_2").empty();
			}
			
			if(data[0].TAG == "0"){
				window.location.href="./error.html";
			}else{
				for(var a=0;a<data.length;a++){
					if(a>30){
					var al = document.getElementById("vips_3_2");
					var div =document.createElement("li");
					div.innerHTML='<label><input type="checkbox" onclick="elect1_3()"  value="'+data[a].ID+'"/>'+data[a].NAME+'</label>'
					al.appendChild(div);
					}
					
				}
			}
		}
	});
}
*/
//工号是否合法
function there_exist(){
	var login_name = document.getElementById('login_name').value;
/*	if (!(/^[0-9A-Za-z_]{5,12}$/.test(login_name))) {
		alert("登录名名格式有误，提示：5-12位字母或字母加数字。");
		return false;
	}	*/
	if (!(/^[0-9]{4,11}$/.test(login_name))) {
		layer.alert("工号名格式有误，提示：4-11位数字。");
		return false;
	}
	$.ajax({
		url: "../../../../service",
		data: {
			proc: "g_xmy.there_exist_driver",
			para: "['" + login_name + "']"
		},
		dataType: "json",
		success: function(data) {
			if(data[0].TAG == "0"){
				document.getElementById("is_verification").innerHTML='<i style="color:red;">账号已存在，请重新输入！</i>';
			}else if(data[0].TAG == "1"){
				document.getElementById("is_verification").innerHTML='<i style="color:green;">账号可以使用！</i>';
			}else{
				layer.alert("请填写登录名");
			}
		}
	});
}

//司机
function up_staff_management_2(){ 
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
	var login_name = document.getElementById('login_name').value;
	var staff_name = document.getElementById('staff_name').value;
	var sj_password = document.getElementById('sj_password').value;
	
	var ac="";
	var count = 0;			
	var a = $("#vips_2").find("input");
	for(var b=0;b<a.length;b++){ 
		if(a[b].getAttribute("type")=="checkbox"){
			if(a[b].checked==true){ 
				ac += a[b].value+",";
			//	console.log("ac===="+ac);
				count++;
			}
		}
	}
	var jurisdiction = ac.substring(0,ac.length-1);//店铺
	//console.log("jurisdiction===="+jurisdiction);
	
	//顺丰网点
	var sf="";
	var count2 = 0;			
	var asf = $("#vips_3").find("input");
//	console.log("顺丰网点asf=="+asf);
	for(var bb=0;bb<asf.length;bb++){ 
		if(asf[bb].getAttribute("type")=="checkbox"){
			if(asf[bb].checked==true){ 
				sf += asf[bb].value+",";
			//	console.log("sf===="+sf);
				count2++;
			}
		}
	}
	var sf_2="";
	var count2_2 = 0;			
	var asf_2 = $("#vips_3_2").find("input");
//	console.log("顺丰网点asf=="+asf);
	for(var bb_2=0;bb_2<asf_2.length;bb_2++){ 
		if(asf_2[bb_2].getAttribute("type")=="checkbox"){
			if(asf_2[bb_2].checked==true){ 
				sf_2 += ","+asf_2[bb_2].value+",";
			//	console.log("sf===="+sf);
				count2_2++;
			}
		}
	}
	var sf_web = sf.substring(0,sf.length-1)+sf_2.substring(0,sf_2.length);
	console.log("sf_web===="+sf_web);
//	console.log("count2===="+count2);
	
	if (staff_name == '') {
		layer.alert("请输入姓名！");
		return false;
	}
	if (!checkChinese(staff_name)) {
		layer.alert("请输入汉字，并且长度为2-8位");
		return false;
	}

	if(jurisdiction == '' && jurisdiction.length<=0&&sf_web == '' && sf_web.length<=0){
		layer.alert("请为司机选择店铺或者网点");
		return false;
	}
/*	if(sf_web == '' && sf_web.length<=0){
		alert("请为司机选择网点");
		return false;
	}
	
	if(count+count2+count2_2>10){		
		alert("一个司机店铺和网点最多只能选择10个");
		return false;
	}
	if(count2>10){		
		alert("一个司机最多只能选择10个顺丰网点");
		return false;
	}
	*/
//	console.log("length==="+jurisdiction.length+"===="+sf_web.length);
	console.log("//////"+username + "','" + login_name + "','" + staff_name + "','" + sj_password  + "','" + jurisdiction + "','" + sf_web + "','" + staff_id);

	$.ajax({
		url: "../../../../service",
		cache : false,
		data : {
			proc : "g_xmy.up_staff_Driver",
			para : "['" + username + "','" + login_name + "','" + staff_name + "','" + sj_password + "','" + jurisdiction + "','" + sf_web + "','" + staff_id + "']"
		},
		dataType : "json",
		success : function(data) {
			console.log(JSON.stringify(data));
			if (data[0].TAG == "1") {
				layer.alert("司机修改成功。",function(index){
					window.location.href="./driver_list.html";
					layer.close(index);
				});
			} else {
				layer.alert("司机修改失败！");
			}
		}
	});
}



//校验只能输入汉字，并且长度为2-4
function checkChinese(name) {
	reg = /^[\u4E00-\u9FA5]{2,8}$/;

    if (reg.test(name)) {
		return true;
    } else {
        return false;
    }
 }
 
 function re(){
	 window.location.href='./driver_list.html';
 }
 
 
 